package com.sino.business.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.sino.bean.Basicinfo;
import com.sino.bean.TblZhuanjialinxuanjieguo;
import com.sino.bean.TblZhuanjiarukuxinxibiao;
import com.sino.bean.TblZhuanjiayuxiangmuguanxi;
import com.sino.business.IExpertCommentService;
import com.sino.dao.IExpertCommentDAO;
import com.sino.dao.IExpertManageDAO;
import com.sino.dao.IPatentManageDAO;
import com.sino.model.BasicInfoModel;
import com.sino.model.ExpertReviewModel;
import com.sino.util.DateUtils;
import com.sino.util.StringUtils;

public class ExpertCommentServiceImp implements IExpertCommentService {
    private IExpertCommentDAO expertCommentDAO;

	private IExpertManageDAO expertManageDAO;

	private IPatentManageDAO patentManageDAO;
	
	public void delComment(String id) {
		this.expertCommentDAO.delComment(id);
		
	}

	public ExpertReviewModel getExpertReview(String id) {
		
		
		return this.getErm(this.expertCommentDAO.findExpertCommentById(id));
	}

	public int getReviewPatentPageCount(String hqlwhere) {
		String hql = "select count(*) from Basicinfo as basicinfo where basicinfo.tzjxm.size>=1";

		if (hqlwhere.length() > 0) {

			hql = hql + " and " + hqlwhere;

		}

		return this.getPatentManageDAO().findNoticePageCount(hql);
	}

	public List getReviewPatentPageList(int firstRow, int maxRow,
			String hqlwhere) {
		String hql = "from Basicinfo as basicinfo where basicinfo.tzjxm.size>=1";

		if (hqlwhere.length() > 0) {
			hql =hql+ " and " + hqlwhere;
		}
		hql=hql+" order by basicinfo.tzjlx.size desc";

		return this.poListToVoList2(this.patentManageDAO.findBasicinfoPageList(
				firstRow, maxRow, hql));

	}

	public List<BasicInfoModel> poListToVoList2(List<Basicinfo> nList) {
		List<BasicInfoModel> tempList = new ArrayList<BasicInfoModel>();
		Iterator<Basicinfo> it = nList.iterator();
		while (it.hasNext()) {
			BasicInfoModel nm = this.getBasicInfoModel(it.next());
			tempList.add(nm);

		}
		return tempList;
	}

	public int saveExpertComment(ExpertReviewModel erm) {
		TblZhuanjialinxuanjieguo tzjlx = new TblZhuanjialinxuanjieguo();
		tzjlx = this.getTzjlx(erm);
		this.expertCommentDAO.saveExpertComment(tzjlx);
		return 0;
	}

	public BasicInfoModel getBasicInfoModel(Basicinfo basicinfo) {
		BasicInfoModel basicInfoModel = new BasicInfoModel();
		List expCommentList = this.getExpertCommentDAO().findExpertComments(
				basicinfo.getZlid());
		if (basicinfo.getTzjlx().size() >=1) {
			basicInfoModel.setAssignState("有评论");// 判断专家评论状态
		} else {
			basicInfoModel.setAssignState("无评论");
		}
		basicInfoModel.setHy(basicinfo.getHy().getId());
		basicInfoModel.setLxdh(basicinfo.getLxdh());
		basicInfoModel.setFmr(basicinfo.getFmr());
		basicInfoModel.setLxr(basicinfo.getLxr());
		basicInfoModel.setName(basicinfo.getName());
		basicInfoModel.setPassw(basicinfo.getPassw());
		basicInfoModel.setQxkwmc(basicinfo.getQxkw().getName());
		basicInfoModel.setShbz(basicinfo.getShbz());
		basicInfoModel.setShrq(basicinfo.getShrq());
		basicInfoModel.setSqrq(basicinfo.getSqrq());
		basicInfoModel.setTxdz(basicinfo.getTxdz());
		basicInfoModel.setXk(basicinfo.getXk().getId());
		basicInfoModel.setYddh(basicinfo.getYddh());
		basicInfoModel.setYzbm(basicinfo.getYzbm());
		basicInfoModel.setZlid(basicinfo.getZlid());
		basicInfoModel.setZlid2(basicinfo.getZlid2());
		basicInfoModel.setZlqr(basicinfo.getZlqr());
		basicInfoModel.setZlrq(basicinfo.getZlrq());
		basicInfoModel.setZxbz(basicinfo.getZxbz());
		basicInfoModel.setSbr(basicinfo.getSbr());
		basicInfoModel.setZlid2Link(this.getZlidLink(basicinfo.getZlid2()));
		return basicInfoModel;
	}
	//专利链接外网处理
	public String getZlidLink(String zlid){
		String outZlidStr=zlid;
		if("zl".equalsIgnoreCase(zlid.substring(0,2))){
			outZlidStr=zlid.substring(2);
		}
		return outZlidStr.replaceAll(" ", "");
		
	}

	public List getExpertComments(String id) {
		List tempList = new ArrayList();
		List expCommentList = this.expertCommentDAO.findExpertComments(id);
		Iterator it = expCommentList.iterator();
		while (it.hasNext()) {
			ExpertReviewModel erm = this.getErm((TblZhuanjialinxuanjieguo) it
					.next());
			tempList.add(erm);
		}
		return tempList;
	}
	public int getReviewPatentPageCount(String hqlwhere, String fbh) {
		String hql = "select count(*) from TblZhuanjiayuxiangmuguanxi as tzjxm where tzjxm.tblZhuanjiarukuxinxibiao.FBh="+fbh;

		if (hqlwhere.length() > 0) {

			hql = hql + " and "+hqlwhere;

		}

		return this.getPatentManageDAO().findNoticePageCount(hql);
	}
// new com.sino.bean.Basicinfo(tzjxm.basicinfo.zlid,tzjxm.basicinfo.zlid2,tzjxm.basicinfo.xk,tzjxm.basicinfo.zltype,tzjxm.basicinfo.qxkw,tzjxm.basicinfo.hy,tzjxm.basicinfo.name,tzjxm.basicinfo.sqrq,tzjxm.basicinfo.zlrq,tzjxm.basicinfo.fmr,tzjxm.basicinfo.zlqr,tzjxm.basicinfo.txdz,tzjxm.basicinfo.yzbm,tzjxm.basicinfo.lxr,tzjxm.basicinfo.lxdh,tzjxm.basicinfo.yddh,tzjxm.basicinfo.shbz,tzjxm.basicinfo.shrq,tzjxm.basicinfo.zjclinfos,tzjxm.basicinfo.sharepeoples,tzjxm.basicinfo.jjxyinfos,tzjxm.basicinfo.zlbhzks,tzjxm.basicinfo.qtzks,tzjxm.basicinfo.zljszks,tzjxm.basicinfo.sqqks,tzjxm.basicinfo.xkqks,tzjxm.basicinfo.passw,tzjxm.basicinfo.sbr,tzjxm.basicinfo.sbrLxdh,tzjxm.basicinfo.zlintro,tzjxm.basicinfo.zlsqrq) 
	public List getReviewPatentPageList(int firstRow, int maxRow, String hqlwhere, String fbh) {
		String hql = "from TblZhuanjiayuxiangmuguanxi  tzjxm  where  tzjxm.tblZhuanjiarukuxinxibiao.FBh="+fbh;
		List tempList=new ArrayList();
		if (hqlwhere.length() > 0) {
			hql = hql+ " and "+hqlwhere;
		}
	   
		List basicList=this.patentManageDAO.findBasicinfoPageList(
				firstRow, maxRow, hql);
		Iterator it=basicList.iterator();
		while(it.hasNext()){
			TblZhuanjiayuxiangmuguanxi tzjxm=(TblZhuanjiayuxiangmuguanxi)it.next();
		    tempList.add(this.getBasicInfoModel(tzjxm.getBasicinfo()));
		    
		     
		}
		return tempList;

	}


	// ----------------------------------------------po and vo
	public ExpertReviewModel getErm(TblZhuanjialinxuanjieguo tzjlx) {
		ExpertReviewModel erm = new ExpertReviewModel();
		erm.setFbh(String.valueOf(tzjlx.getTblZhuanjiarukuxinxibiao()
						.getFBh()));
		erm.setZjname(tzjlx.getTblZhuanjiarukuxinxibiao().getXm());
		erm.setPid(tzjlx.getPid());
		erm.setLxzt(tzjlx.getLxzt());
		erm.setZlname((tzjlx.getBasicinfo().getName()));
		erm.setZlid((tzjlx.getBasicinfo().getZlid()));
		erm.setLxrq(tzjlx.getLxrq());
		erm.setZhjyj(tzjlx.getZhjyj());
		erm.setZhjyjinfo(StringUtils.getZnOfomit(60,tzjlx.getZhjyj()));

		return erm;
	}

	public TblZhuanjialinxuanjieguo getTzjlx(ExpertReviewModel erm) {
		TblZhuanjialinxuanjieguo tzjlx = new TblZhuanjialinxuanjieguo();
		Basicinfo basicinfo = new Basicinfo();
		TblZhuanjiarukuxinxibiao tzjb = new TblZhuanjiarukuxinxibiao();
		tzjb.setFBh(Integer.valueOf(erm.getFbh()));
		basicinfo.setZlid(erm.getZlid());
		tzjlx.setTblZhuanjiarukuxinxibiao(tzjb);
		tzjlx.setPid(erm.getPid());
		tzjlx.setLxzt(erm.getLxzt());
		tzjlx.setBasicinfo(basicinfo);
		tzjlx.setLxrq(DateUtils.getTimestamp());
     
		tzjlx.setZhjyj(erm.getZhjyj());
		

		return tzjlx;

	}

	// --------------------------------------------- get or set

	public IExpertCommentDAO getExpertCommentDAO() {
		return expertCommentDAO;
	}

	public void setExpertCommentDAO(IExpertCommentDAO expertCommentDAO) {
		this.expertCommentDAO = expertCommentDAO;
	}

	public IExpertManageDAO getExpertManageDAO() {
		return expertManageDAO;
	}

	public void setExpertManageDAO(IExpertManageDAO expertManageDAO) {
		this.expertManageDAO = expertManageDAO;
	}

	public IPatentManageDAO getPatentManageDAO() {
		return patentManageDAO;
	}

	public void setPatentManageDAO(IPatentManageDAO patentManageDAO) {
		this.patentManageDAO = patentManageDAO;
	}


}
